package de.koller.gravity.scene.universe;

import de.koller.aframe.plain.PlainQuality;
import de.koller.aframe.util.misc.ATimer;
import de.koller.gravity.mech.particle.ParticleEngine;
import de.koller.gravity.mech.particle.environment.IndependentMovement;
import de.koller.gravity.mech.particle.render.DottedRenderer;
import de.koller.gravity.mech.universe.GravitationEnvironment;
import de.koller.gravity.mech.universe.MoltPlanet;
import de.koller.gravity.mech.universe.PlanetaryMass;
import de.koller.gravity.scene.Scene;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class UniverseCreation extends Scene {

	public final ATimer timer = new ATimer( 17 );
	public ParticleEngine particleEngine;

	public UniverseCreation() {
		initThis();
	}
	
	private void initThis() {
		particleEngine = new ParticleEngine();
		particleEngine.renderers.add( new DottedRenderer() );
		
		particleEngine.environments.add( new GravitationEnvironment() );
		particleEngine.environments.add( new MoltPlanet() );
		particleEngine.environments.add( new IndependentMovement() );
		
		for( int y = 0; y < 4; y++ )
			for( int x = 0; x < 4; x++ ) {
				PlanetaryMass p1 = new PlanetaryMass( .1 );
				p1.position.x = 60*x;
				p1.position.y = 60*y;
				//p1.velocity.y = 0.01;
				particleEngine.particles.add( p1 );
			}
	}

	@Override
	public void initSequence() {
		super.initSequence();
		
		this.initThis();
	}
	
	@Override
	public void renderSceneFast( Graphics2D g, int elapsedTime ) {
		super.renderSceneFast( g, elapsedTime );
	
		PlainQuality.worst( g );
		
		AffineTransform a = g.getTransform();
		g.translate( groot.display.width/2, groot.display.height/2 );
		
		if( timer.step( elapsedTime ) )
			for( int i = 0; i < 100; i++ )
				particleEngine.step();

		particleEngine.renderFast( g );
		
		g.setTransform( a );
	}

//	@Override
//	public BufferedImage renderScene() {
//		BufferedImage frame = new BufferedImage( 1920, 1080, BufferedImage.TYPE_INT_RGB );
//		Graphics2D g = frame.createGraphics();
//		
//		PlainQuality.better( g );
//		
//		AffineTransform a = g.getTransform();
//		g.scale( 2, 2 );
//		g.translate( 1920/4, 1080/2 - 50 );
//		particleStep( g );
//
//		List<Particle> ps = new ArrayList<>();
//		particleEngine.particles.addAllTo( ps );
//		
//		Collections.sort( ps, new Comparator<Particle> () {
//			@Override public int compare( Particle o1, Particle o2 ) {
//				return Long.compare( o1.creationTime, o2.creationTime );
//			}
//		} );
//		
//		particleEngine.qualityRendering( g, ps );
//		g.setTransform( a );
//		
//		g.dispose();
//		frame.flush();
//		return frame;
//	}

}
